package com.tcs.channel.init;

import com.tcs.channelHandler.ServerChannelHeadler;
import com.tcs.coder.decoder.MyDecoder;
import com.tcs.coder.encoder.MyEncoder;
import com.tcs.model.ipdata.IpDataBean;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.Charset;

public class ServerChannelInit extends ChannelInitializer {

    private final Logger logger = LoggerFactory.getLogger(ServerChannelInit.class);

    @Override
    protected void initChannel(Channel ch) throws Exception {
        logger.debug("初始化通道参数");
//        ByteBuf byteBuf = Unpooled.copiedBuffer("$".getBytes());
//        ch.pipeline().addLast("", new DelimiterBasedFrameDecoder(1024 * 60 , byteBuf));
//        ch.pipeline().addLast("lineBasedFrameDecoder" ,new LineBasedFrameDecoder(65 * 1024));
        // 编码器
        ch.pipeline().addLast(new MyDecoder(IpDataBean.class));
        ch.pipeline().addLast(new MyEncoder(IpDataBean.class));
        // 编码格式
        ch.pipeline().addLast("encoder_GBK", new StringEncoder(Charset.forName("GBK")));
        //解码格式
        ch.pipeline().addLast("decoder_UTF_8",new StringDecoder(Charset.forName("UTF-8")));
        ch.pipeline().addLast("channelHandler" , new ServerChannelHeadler());
    }
}
